import {createRouter, createWebHistory} from 'vue-router'
import ListComponent from '@/components/ListComponent'
import FillComponent from '@/components/FillComponent'
import DataComponent from '@/components/DataComponent'
import EditComponent from '@/components/EditComponent'
import DatepickerComponent from '@/components/DatepickerComponent'
import LoginComponent from '@/components/LoginComponent'
import UserProfileComponent from '@/components/UserProfileComponent'
import ResetPasswordComponent from '@/components/ResetPasswordComponent'
import AdminApplicationsComponent from '@/components/AdminApplicationsComponent'
import UserManagementComponent from '@/components/UserManagementComponent'
import BlogListComponent from '@/components/BlogListComponent.vue'
import BlogEditComponent from '../components/BlogEditComponent.vue'
import BlogViewComponent from '@/components/BlogViewComponent.vue'
import HomeComponent from '@/components/HomeComponent'

const routes = [
    {
        path: '/login',
        name: 'LoginComponent',
        component: LoginComponent
    },
    {
        path: '/',
        name: 'Home',
        component: HomeComponent
    },
    {
        path: '/questionnaires',
        name: 'ListComponent',
        component: ListComponent,
        meta: { requiresAuth: true }
    },
    {
        path: '/fill/:id',
        name: 'FillComponent',
        component: FillComponent,
        meta: { requiresAuth: true }
    },
    {
        path: '/data/:id',
        name: 'DataComponent',
        component: DataComponent,
        meta: { 
            requiresAuth: true,
            requiresAdmin: true
        }
    },
    {
        path: '/edit/:id',
        name: 'EditComponent',
        component: EditComponent,
        meta: { requiresAuth: true }
    },
    {
        path: '/datepicker',
        name: 'DatepickerComponent',
        component: DatepickerComponent,
        meta: { requiresAuth: true }
    },
    {
        path: '/profile',
        name: 'UserProfile',
        component: UserProfileComponent,
        meta: { requiresAuth: true }
    },
    {
        path: '/reset-password',
        name: 'ResetPassword',
        component: ResetPasswordComponent
    },
    {
        path: '/admin/applications',
        name: 'AdminApplications',
        component: AdminApplicationsComponent,
        meta: { 
            requiresAuth: true,
            requiresAdmin: true
        }
    },
    {
        path: '/admin/users',
        name: 'UserManagement',
        component: UserManagementComponent,
        meta: { 
            requiresAuth: true,
            requiresAdmin: true
        }
    },
    {
        path: '/blog',
        name: 'BlogList',
        component: BlogListComponent,
        meta: { requiresAuth: true }
    },
    {
        path: '/blog/edit',
        name: 'BlogEdit',
        component: BlogEditComponent
    },
    {
        path: '/blog/edit/:id',
        name: 'BlogEditWithId',
        component: BlogEditComponent
    },
    {
        path: '/blog/view/:id',
        name: 'BlogView',
        component: BlogViewComponent,
        meta: { requiresAuth: true }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach((to, from, next) => {
    const currentUser = JSON.parse(localStorage.getItem('currentUser') || '{}')
    
    if (to.path === '/login' && currentUser.username) {
        next('/');
        window.location.reload();
        return;
    }
    
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // 检查是否登录
        if (!currentUser.username) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        } 
        // 检查是否需要管理员权限
        else if (to.matched.some(record => record.meta.requiresAdmin) && !currentUser.isAdmin) {
            alert('需要管理员权限')
            next('/')
        }
        else {
            next()
        }
    } else {
        next()
    }
});

export default router
